﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFTools;
using Path = System.IO.Path;

namespace CSharpToolWPF.Pages {
    /// <summary>
    /// PageCheckNotAddedProjects.xaml 的交互逻辑
    /// </summary>
    public partial class PageCheckNotAddedProjects : Page {
        private static PageCheckNotAddedProjects page;
        public static PageCheckNotAddedProjects Instance {
            get;
            set;
        }
        private TaskCompletionSource<bool> taskCompletionSource = new();
        private Color colorAlert = Color.FromRgb(237, 87, 85);
        private Color colorWarning = Color.FromRgb(237, 173, 85);
        private Color colorOk = Color.FromRgb(167, 237, 85);
        // 
        private string slnPath = "D:/VC/ObjectPRX 2024/sln";
        public PageCheckNotAddedProjects() {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e) {
            // 获取文件夹中所有的项目
            var listDirs = Directory.GetDirectories(slnPath).ToList();
            var listProjectName = listDirs.Select(item=>Path.GetFileName(item)).ToList();
            // 获取解决方案中所有的项目
            string slnContent = File.ReadAllText(tbSlnPath.Text);
            string pattern = @"""([^""]*)""";
            MatchCollection matches = Regex.Matches(slnContent, pattern);
            var listProjectsInSln = matches.Where(item => !item.Value.StartsWith("\"{") && !item.Value.StartsWith("\".")).Select(item => item.Value.Replace("\"","")).ToList();
            // 计算差异
            var listNotAdded = listProjectName.Except(listProjectsInSln).ToList();
            // 输出
            foreach(var item in listNotAdded) {
                Logger.WriteLog($"{item}\n", rtbLog, newParagraph: false);
            }
        }

        private void btnPickSln_Click(object sender, RoutedEventArgs e) {
            var fileDialog = new OpenFileDialog();
            //fileDialog.FileName = "Document"; // Default file name
            fileDialog.DefaultExt = ".sln"; // Default file extension
            fileDialog.Filter = "VS Solution (.sln)|*.sln"; // Filter files by extension

            // Show open file dialog box
            bool? result = fileDialog.ShowDialog();
            string file = "";
            // Process open file dialog box results
            if (result == true) {
                // Open document
                file = fileDialog.FileName;
            }
            tbSlnPath.Text = file;
        }

        private void btnContinue_Loaded(object sender, RoutedEventArgs e) {
            btnContinue.Visibility = Visibility.Collapsed;
        }

        private void btnContinue_Click(object sender, RoutedEventArgs e) {
            taskCompletionSource.SetResult(true);
            btnContinue.Visibility = Visibility.Collapsed;
        }
    }
}
